<?php
session_start();
// 如果session中不存在 username 就表示登录
if(!isset($_SESSION['username'])){
	// header("location: login.php");
	// die("未登录<script>setTimeout(function(){window.location.href='login.php'},3000)</script>");}
	die("<script>alert('未登录'); window.location.href='../login.php';</script>");
	
}

include("../conn.php");

// 连接数据库
$pdo = getConn();

//查询账号密码
$sql = "select * from articles";

// 预编译sql语句
$stmt = $pdo->prepare($sql);

// 执行sql语句
$stmt->execute();

// 获取所有数据
$articles = $stmt->fetchAll();

// print_r($articles);

// foreach($articles as $article) {
// 	echo $article['title']."\n";
// }

?>
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<a href="./add.php">添加文章</a>
	<a href="../logout.php" onclick="return confirm('是否确认退出登录')">退出登录</a>
	<ul>
		<?php 
		foreach($articles as $article) {
		?>
		<li><a href="/blog.php?id=<?=$article['id']?>">
		<?php
		echo $article['title'];
		?>
		</a>  
		
		<?=$article['ctime']?>
		</li>
		<?php 
		}
		?>
	</ul>
</body>
</html>

